package com.zy.imagineCommunity.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.fastjson.JSONObject;
import com.zy.imagineCommunity.dao.MessageMapper;
import com.zy.imagineCommunity.entity.Friends;
import com.zy.imagineCommunity.entity.Message;
import com.zy.imagineCommunity.entity.MessageText;
import com.zy.imagineCommunity.entity.Page;
import com.zy.imagineCommunity.service.FriendsService;
import com.zy.imagineCommunity.service.MessageService;
import com.zy.imagineCommunity.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class messageController {

    @Autowired
    UserSerivce userSerivce;

    @Autowired
    FriendsService friendsService;

    @Autowired
    MessageService messageService;

    @GetMapping("/message/{toId}")
    public String getMessage(Model model, @PathVariable("toId") int toId) {
        model.addAttribute("toId", toId);
        System.out.println(userSerivce.queryUserByUserId(toId));
        return "/message";
    }

    @ResponseBody
    @GetMapping("/addMessage")
    public void addMessage(String message, int fromId, int toId) {
        System.out.println(fromId + "要发消息给" + toId);
        Friends friends = friendsService.queryFriendsByUserIdAndParentId(fromId, toId);

        friendsService.updateFriendsById(new Friends(friends.getId(), friends.getUserId(), friends.getParentId(), friends.getStatus(), friends.getMessageNum() + 1));

        String fromToId = null;

        if (fromId < toId) {
            fromToId = fromId + "_" + toId;
        } else if (fromId > toId) {
            fromToId = toId + "_" + fromId;
        }

        System.out.println(fromToId);

        //创建时间
        Date date = new Date();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        messageService.addMessage(new Message(null, fromId, toId, fromToId, message, dateFormat.format(date)));
    }

    @ResponseBody
    @GetMapping("/messageList")
    public JSONObject messageList(int fromId, int toId, int pageNo) {
        JSONObject jsonObject = new JSONObject();

        System.out.println("请求fromId:" + fromId);
        System.out.println("请求toId:" + toId);

        Friends friends = friendsService.queryFriendsByUserIdAndParentId(toId, fromId);

        friendsService.updateFriendsById(new Friends(friends.getId(), friends.getUserId(), friends.getParentId(), friends.getStatus(), 0));

        String fromToId = null;

        if (fromId < toId) {
            fromToId = fromId + "_" + toId;
        } else if (fromId > toId) {
            fromToId = toId + "_" + fromId;
        }

        Page<MessageText> page = messageService.queryFriendsByParentIdAndStatusAndPageNoAndPageSize(fromToId, pageNo, 10);

        jsonObject.put("page", page);

        return jsonObject;
    }

    @ResponseBody
    @GetMapping("/deleteMessage")
    public void deleteMessage(int messageId) {
        System.out.println("删除消息");
        messageService.deleteMessageByMessageId(messageId);
    }

}
